/**
 * zmn文章模块-文章列表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author daizc
 * @since 2018/10/18
 */
window.ids = [];
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function () {
    window.$ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let laypage = layui.laypage;
    let layedit = layui.layedit;
    let laydate = layui.laydate;
    let cols = [
        [
            {type: 'checkbox', fixed: 'left', width: 50, align: 'center'},
            {field: 'categId', title: 'ID', width: 80, align: 'center', unresize: true, sort: false},
            // {type: 'numbers', title: '序号', width: 40, align: 'center', unresize: true, sort: false},
            {field: 'name', title: '配件一级分类', align: 'center'},
            // {field: 'fullName', title: '分类全名', align: 'center'},
            // {title: '编辑时间', width: 200, align: 'center', unresize: true, templet: '<div>{{zmnUtil.createTime(d.updateTime)}}</div>'},
            // {field: 'updater', title: '编辑用户', width: 200, align: 'center', unresize: true},
            {field: 'status', title: '状态', width: 80, align: 'center', unresize: true, templet: '<div>{{zmnEnum.STATUS[d.status]}}</div>'},
            {field: 'creater', title: '创建人', width: 80, align: 'center'},
            {field: 'createTime',title: '创建时间', width: 150, align: 'center', templet: '<div>{{zmnUtil.createTime(d.createTime)}}</div>'},
            {fixed: 'right', title: '操作', width: 160, align: 'center', toolbar: '#zmn-part-category-first-table-bar'}
        ]
    ];

    laydate.render({
        elem: '#updateStartEndDate'
        , range: true
    });

    tableRender("");

    function tableRender(data) {
        zmnLayUtil.layTableRender(table, cols, "zmn-part-category-first-table", data, "/part/category/first/ajax/list.action");
    }

    //监听表单提交
    form.on('submit(query-submit)', function (data) {
        zmnMothedUtil.deleteEmptyProperty(data.field);
        tableRender(data.field);
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });


    //【表格右侧工具栏监听】
    table.on('tool(zmn-part-category-first-table)', function (obj) {
        let categId = obj.data.categId;
        if (obj.event === 'enable') {
            $.post("/part/category/first/ajax/enable.action", {categId: categId}).done(function () {
                table.reload("zmn-part-category-first-table");
            });
        } else if (obj.event === 'disable') {
            layer.confirm('真的要禁用吗？', function (index) {
                $.post("/part/category/first/ajax/disable.action", {categId: categId}).done(function () {
                    table.reload("zmn-part-category-first-table");
                });
                layer.close(index);
            });

        } else if (obj.event === 'detail') {
            zmnLayUtil.openIframeCustom(layer, "/part/category/first/addEdit.action?categId=" + categId, {
                title: "编辑配件一级分类", area:["400px", "300px"]
            });
        }
    });

    //【表格上方工具栏监听】
    table.on("toolbar(zmn-part-category-first-table)", function (obj) {
        if (obj.event === "add") {
            zmnLayUtil.openIframeCustom(layer, "/part/category/first/addEdit.action", {
                title: "新增配件一级分类", area:["400px", "350px"]
            });

        } else if (obj.event === "start" || obj.event === "stop") {
            let ids = table.checkStatus('zmn-part-category-first-table').data.map((item) => item.categId);
            if (ids.length < 1) {
                layer.msg('请选择至少一个要操作的配件分类', {icon: 2});
                return false;
            }

            zmnLayUtil.tableEnableDisable($, obj, "/part/category/first/ajax/updateStatus.action", ids, function (data) {
                table.reload("zmn-part-category-first-table");
            });
        }


        return false;


    });

    //单元格编辑
    table.on('edit(zmn-part-category-first-table)', function (obj) {

        $.post("/part/category/first/editname.action", {categId: obj.data.categId, name: obj.data.name}).done(
            function (obj) {
                layer.alert(obj.message);
                table.reload("zmn-part-category-first-table");
            });
    });
});